package im.starchat.chat.activity.profile;

import android.os.Bundle;
import android.os.SystemClock;
import android.text.TextUtils;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.Toast;

import im.starchat.chat.R;
import im.starchat.chat.XLApplication;
import im.starchat.chat.activity.BaseActivity;
import im.starchat.chat.rest.model.api.NullResponse;
import im.starchat.chat.utils.PreferencesStorageUtil;

import butterknife.ButterKnife;
import butterknife.InjectView;
import im.starchat.chat.utils.RetrofitErrorUtil;
import retrofit.Callback;
import retrofit.RetrofitError;
import retrofit.client.Response;


/**
 * 申请入驻界面
 */
public class ApplyActivity extends BaseActivity implements View.OnClickListener{

    @InjectView(R.id.apply_back)
    ImageView mBackBtn;
    @InjectView(R.id.invitate_edt)
    EditText mInvitateEdt;
    @InjectView(R.id.apply_btn)
    Button mApplyBtn;
    @InjectView(R.id.debug_mode_btn)
    LinearLayout mDebugBtn;

    private long firstClickTime = 0;
    private int mClickSum = 0;

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_apply);
        ButterKnife.inject(this);
        mBackBtn.setOnClickListener(this);
        mApplyBtn.setOnClickListener(this);
        mDebugBtn.setOnClickListener(this);
    }


    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.apply_back:
                finish();
                break;
            case R.id.apply_btn:
                apply();
                break;
            case R.id.debug_mode_btn:
                if (firstClickTime > 0) {
                    long secondClickTime = SystemClock.uptimeMillis();
                    long dtime = secondClickTime - firstClickTime;
                    if (dtime < 2000) {
                        mClickSum++;
                        if (mClickSum >= 10) {
                            toast("打开调试模式", Toast.LENGTH_SHORT);
                            PreferencesStorageUtil.setDebugMode(ApplyActivity.this, true);
                        }
                    } else {
                        firstClickTime = SystemClock.uptimeMillis();
                        mClickSum = 1;
                    }
                    return;
                } else {
                    firstClickTime = SystemClock.uptimeMillis();
                    mClickSum = 1;
                }

                break;
        }
    }

    public void apply(){
        if(!TextUtils.isEmpty(mInvitateEdt.getText().toString())){
            showProgressDialog();
            XLApplication.getInstance().getRestClient().apply(mInvitateEdt.getText().toString() , new Callback<NullResponse>(){
                        @Override
                        public void success(NullResponse loginResponse, Response response) {
                            dismissProgressDialog();
                            if(RetrofitErrorUtil.handleResponse(ApplyActivity.this, response)){
                                return;
                            }
                            toast("认证成功!", Toast.LENGTH_SHORT);
                            mInvitateEdt.setText("");
                            PreferencesStorageUtil.setStar(ApplyActivity.this, true);
                            finish();
                        }

                        @Override
                        public void failure(RetrofitError error) {
                            dismissProgressDialog();
                            toast("认证失败，请确认邀请码是否正确", Toast.LENGTH_SHORT);
                        }
                    });
        }else{
            toast("请输入邀请码", Toast.LENGTH_SHORT);
        }
    }

}
